#ifndef TIMER_H
#define TIMER_H

#include <memory>

#include "callbacks.h"
#include "TimeStamp.h"
#include "Atomic.h"

class Timer {
public:
    Timer(TimerCallback cb, TimeStamp when, double interval) :
        timerCallback_(std::move(cb)),
        expiration_(when),
        interval_(interval),
        repeat_(interval > 0.0),
        sequence_(s_numCreated_.incrementAndGet())
    {
    }

    void run() {
        timerCallback_();
    }

    TimeStamp getExpiration() { return expiration_; }
    
    bool isRepeat() { return repeat_; }

    int64_t getSequence() { return sequence_; }

    // 从when开始计算，interval_秒后到时
    void restart(TimeStamp when);
    

private:
    TimerCallback timerCallback_;
    TimeStamp expiration_;
    const double interval_;
    const bool repeat_;
    const int64_t sequence_;

    static AtomicInt64 s_numCreated_;
};

typedef std::shared_ptr<Timer> TimerPtr;


#endif